/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.niedziela12.pai.ejb.reservation.managers;

import com.niedziela12.pai.ejb.dto.ReservationDTO;
import com.niedziela12.pai.ejb.entities.Reservation;
import com.niedziela12.pai.ejb.reservation.facades.ReservationFacadeLocal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

/**
 *
 * @author Kamil
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class ReservationManager implements ReservationManagerLocal {

    @EJB
    private ReservationFacadeLocal reservationFacade;

    @Override
    public void addNewReservation(ReservationDTO reservation) {
        reservationFacade.create(reservation.toReservation());
    }

    @Override
    public void updateReservationInfo(ReservationDTO reservation) {
        reservationFacade.edit(reservation.toReservation());
    }

    @Override
    public void deleteReservation(ReservationDTO reservation) {
        reservationFacade.remove(reservation.toReservation());
    }

    @Override
    public List<ReservationDTO> getReservations(int from, int to) {
        List<Reservation> res = reservationFacade.findRange(new int[]{from,to});
        List<ReservationDTO> dto = new ArrayList<ReservationDTO>();
        for(Reservation r : res)dto.add(new ReservationDTO(r));
        return dto;
    }

    @Override
    public List<ReservationDTO> getAllReservations() {
        List<Reservation> res = reservationFacade.findAll();
        List<ReservationDTO> dto = new ArrayList<ReservationDTO>();
        for(Reservation r : res)dto.add(new ReservationDTO(r));
        return dto;
    }

    @Override
    public int getReservationsCount() {
        return reservationFacade.count();
    }

    @Override
    public ReservationDTO getReservation(Long id) {
        return new ReservationDTO(reservationFacade.find(id));
    }

    @Override
    public List<ReservationDTO> getReservationForUser(String login) {
        List<Reservation> res = reservationFacade.findUserReservation(login);
        List<ReservationDTO> dto = new ArrayList<ReservationDTO>();
        for(Reservation r : res)dto.add(new ReservationDTO(r));
        return dto;
    }
    
}
